/*
 * @Description: userApi main
 * @Author: zhaokun
 * @Date: 2021-12-29 14:48:29
 * @FilePath: \user\api\user.go
 */
package main

import (
	"flag"
	"fmt"
	"net/http"

	"user/api/internal/config"
	"user/api/internal/handler"
	"user/api/internal/svc"
	"user/common"

	"github.com/tal-tech/go-zero/core/conf"
	"github.com/tal-tech/go-zero/rest"
	"github.com/tal-tech/go-zero/rest/httpx"
)

var configFile = flag.String("f", "etc/user-api.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c, conf.UseEnv())
	// fmt.Println(c.RedisConfig.Addr)

	ctx := svc.NewServiceContext(c)
	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	handler.RegisterHandlers(server, ctx)

	// custom error
	httpx.SetErrorHandler(func(err error) (int, interface{}) {
		switch e := err.(type) {
		case *common.NwError:
			return http.StatusOK, e.Data()
		default:
			return http.StatusInternalServerError, nil
		}
	})
	// user := make([]models.User, 0)
	// ctx.Model.Where("1=1").Asc("id").Limit(15, 15).Find(&user)
	// fmt.Println(user)
	// redisConf := &common.RedisConf{
	// 	Addr:     "127.0.0.1:6379",
	// 	Db:       0,
	// 	Password: "",
	// }
	// cache := common.NewNwCacheUtil(redisConf)
	// num := cache.Set("name", "zhoakun", 0)
	// fmt.Println(num)
	// name := cache.Get("name")
	// fmt.Println(name)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
